package com.zhuqieli.database_learn.controller;

import com.aliyun.oss.OSSException;
import com.zhuqieli.database_learn.config.OSSConfig;
import com.zhuqieli.database_learn.enummeration.Result;
import com.zhuqieli.database_learn.enummeration.ResultEnum;
import com.zhuqieli.database_learn.pojo.FileTable;
import com.zhuqieli.database_learn.service.FileTableService;
import com.zhuqieli.database_learn.utils.RedisUtils;
import com.zhuqieli.database_learn.utils.UploadUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author dupr
 * @description 文件服务接口
 * @date 2023/8/30 19:47:08
 */
@RestController
@Slf4j
@RequestMapping("/file")
public class FileTableController {
    @Autowired
    FileTableService fileTableService;

    @Autowired
    RedisUtils redisUtils;
    @Resource
    private OSSConfig ossConfig;

    @Autowired
    UploadUtils uploadUtils;

    private String bucketName = "xucfglodon";

    //上传文件
    @PostMapping("/upload")
    public Result uploadFile(@RequestParam(value = "file") MultipartFile[] myfiles,
                             @RequestParam(value = "userId") Integer userid,
                             @RequestParam(value = "username") String username) throws IOException {
        if (myfiles.length == 0) {
            return Result.error(ResultEnum.ERROR);
        }
        List<FileTable> fileInfosAndUploadToOSS = uploadUtils.getFileInfosAndUploadToOSS(myfiles, userid, username);
        fileTableService.uploadFileTable(fileInfosAndUploadToOSS, userid, username);
        return Result.success("上传文件成功");
    }

    //下载文件（返回url下载到客户端）
    @GetMapping("/downloadUrl")
    public Result downloadUrl(@RequestParam(value = "username") String username,
                              @RequestParam(value = "filename") String filename) {
        return Result.success(fileTableService.downloadUrl(username, filename));
    }

    //展示预览图
    @GetMapping("/preViewUrl")
    public Result preViewUrl(@RequestParam(value = "username") String username,
                             @RequestParam(value = "filename") String filename) {
        return Result.success(fileTableService.preViewUrl(username, filename));
    }

    //下载文件（直接下载到服务器）
    @ApiOperation("下载文件")
    @PostMapping("/download")
    public Result download(@RequestParam(value = "account") String account,
                           @RequestParam(value = "fileId") String fileId) {
        fileTableService.fileDownload(account, fileId);
        return Result.success();
    }

    //设置文件公开
    @PostMapping("/publicFile")
    public Result setFilePublic(String[] snowflakeIds) {
        fileTableService.setFileListPublic(snowflakeIds);
        return Result.success();
    }

    //设置文件删除
    @PostMapping("/deleteFile")
    public Result setFileDeleted(@RequestParam(value = "fileId") String fileId ){
        fileTableService.setFileListDeleted(fileId);
        return Result.success();
    }

    //用户查看回收站
    @GetMapping("/getMyDeleted")
    public Result getMyDeleted(int userId) {
        List<FileTable> fileTables = fileTableService.selectUserRecycle(userId);

        return Result.success(fileTables);
    }

    //还原回收站文件
    @PostMapping("/rollBackFile")
    public Result rollBackFile(String snowflakeId) {
        fileTableService.rollbackFileDeleted(snowflakeId);
        return Result.success();
    }

    //修改文件信息
    @PostMapping("/update")
    public Result updateFile(FileTable fileTable) {
        fileTableService.upDateFileTable(fileTable);
        return Result.success();
    }

    //查看我的文件
    @GetMapping("/getMyFile")//已刪除的不显示
    public Result getMyFile(Integer userId) {
        List<FileTable> fileTables = fileTableService.selectMyfiles(userId);
        return Result.success(fileTables);
    }

    //查看公开的文件
    @GetMapping("/getPublic")
    public Result getPublic(int current, int size) {
        List<FileTable> fileTables = fileTableService.selectAllPublicFiles(current, size);
        return Result.success(fileTables);
    }


    //  测试用
    @ApiOperation("删除文件")
    @DeleteMapping("/delete")
    public void delete(String objectName) {
        fileTableService.fileDelete(objectName);
    }

    //  测试用
    @ApiOperation("修改文件名称")
    @DeleteMapping("/renameFile")
    public void renameFile(String objectName, String newObjectName) throws OSSException {
        fileTableService.fileRename(objectName, newObjectName);
    }

    //  测试用
    @ApiOperation("获取文件大小")
    @GetMapping("/getFileSize")
    public void getFileSize(@RequestParam(value = "account") String account,
                            @RequestParam(value = "fileId") String fileId) {
        fileTableService.fileSizeShow(account, fileId);
    }
}
